package com.he.User.UserController;

import com.he.User.UserEntity.User;
import com.he.User.UserService.UserService;
import com.sun.deploy.net.HttpResponse;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

/**
 * @author 贺文杰
 * 2018/8/19 14:38
 */
@Controller
@RequestMapping("user")
public class UserController {

    //加了个注释


    @Autowired
    private UserService service;


    @RequestMapping("/denlu")
    public String denle(){
        return "login";
    }
    @RequestMapping("/check")
    @ResponseBody
    public String checkLogin(@RequestParam("username") String username, @RequestParam("password") String password, HttpServletRequest request ,HttpServletResponse response ) throws UnsupportedEncodingException, NoSuchAlgorithmException {

        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("P3P", "CP=CAO PSA OUR");
        if (request.getHeader("Access-Control-Request-Method") != null && "OPTIONS".equals(request.getMethod())) {
            response.addHeader("Access-Control-Allow-Methods", "POST,GET,TRACE,OPTIONS");
            response.addHeader("Access-Control-Allow-Headers", "Content-Type,Origin,Accept");
            response.addHeader("Access-Control-Max-Age", "120");
        }
        //查数据库
        User user1= service.selectByName(username);
        System.out.println("要查询的username:"+username+"密码:"+password);
        System.out.println("数据库中的username"+user1.getUsername()+"密码"+user1.getPassword());
        if(user1!=null){
            if(user1.getPassword().equals(password)){
//                //登陆成功,设置session
//                request.getSession().setAttribute("userinfo",user1);
                System.out.println("login_succ");
                return "login_succ";
            }
            else{
                System.out.println("login_fail");
                return "login_fail";
            }
        }else{
            //校验失败 返回校验失败signal
            System.out.println("login_succ");
            return "login_fail";
        }
    }
}
